'''
Created on Aug 28, 2012

@author: kingsfield
'''
import string
#import spell_corrector
from util import Constant
#from nltk.corpus import stopwords
from datetime import datetime
#from Constant import block, MAX_BLOCK

#english_stops = set(stopwords.words('english'))

exclude = set(string.punctuation)

test_correct = dict()

with open('/home/kingsfield/Desktop/BestBuyPc/words_spell_test_check_2.csv') as fr:
    data = fr.readlines()
for line in data:
    line = line[0:-1]
    ss = line.split(',;')
    if ss[1] != '' and len(ss[1]) >= 1:
        if ss[1][-1] == ',':
            ss[1] = ss[1][0:-1]
        test_correct[ss[0]] = ss[1]


def get_date(click_time):
    try:
        t = datetime.strptime(click_time, '%Y-%m-%d %H:%M:%S.%f')
    except:
        t = datetime.strptime(click_time, '%Y-%m-%d %H:%M:%S')
    return t

def train_parse(line):
    ss = line.split(',')
    uid = ss[0]
    iid = ss[1]
    cat = ss[2]
    query = ss[3]
    clickt = ss[4]
    queryt = ss[5]
    return uid, iid, cat, query, clickt, queryt

class wordutil(object):
    hit_count = 0
    '''
    classdocs
    '''
    def __init__(self, params):
        '''
        Constructor
        '''
    
    @classmethod
    def is_legal(cls, word):
        #if word.find(' ') > 0 or len(word) <= 0 or word in english_stops:
        if word.find(' ') > 0 or len(word) <= 0 or word in Constant.stopwords:
            return False
        else:
            return True

#    @classmethod
#    def corret(cls, w):
#        if w in spell_corrector.spell_dict:
#            return spell_corrector.spell_dict[w]
#        else:
#            return w
    
    @classmethod
    def get_correct_words(cls, raw):
        words = cls.get_words(raw)
        
        #words = [cls.corret(w) for w in words]
        words = ' '.join(words)
        if words.find('x box') >= 0:
            words = words.replace('x box', 'xbox')
        words = words.split(' ')
        
        words = [w for w in words if cls.is_legal(w)]
        return words

    @classmethod
    def get_correct_test_query(cls, query):
        words = cls.get_correct_words(query)
        q = ' '.join(words)
        if q in test_correct:
#            cls.hit_count += 1
#            print 'hit=%s' % cls.hit_count
            return test_correct[q]
        else:
            return q

    @classmethod
    def get_words(cls, raw):
        raw.strip()
        raw = raw.lower()
        res = ''
        for ch in raw:
            res += ch if ch not in exclude else ' '
        res = res.split(' ')
        words = [w for w in res if cls.is_legal(w)]
        return words

if __name__ == '__main__':
    #print wordutil.get_correct_words('Just dance 3 x box the gta war3 aaa')
#    with open('/home/kingsfield/Desktop/BestBuyPc/words_spell_test_check_2.csv') as fr:
#        data = fr.readlines()
#    for line in data:
#        line = line[0:-1]
#        ss = line.split(',;')
#        if ss[1] == '':
#            print line
    print 'done'
